package com.company.book_store.web.servlet;

import com.alibaba.fastjson.JSON;
import com.company.book_store.pojo.User;
import com.company.book_store.service.Iuserservice;
import com.company.book_store.service.UserserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register")
public class Register extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Iuserservice service = new UserserviceImpl();
        Map<String,Integer>map = new HashMap<>();
        PrintWriter out = resp.getWriter();
        String account = req.getParameter("account");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String telephone = req.getParameter("telephone");
        User user = new User(account,password,email,telephone,username);
        System.out.println(user);
       int i = service.insertUser(user);
        System.out.println(i);
       if(i == 1){
           HttpSession session =req.getSession();
           session.setAttribute("username",user);
          map.put("massage",1);
       }
       else map.put("massage",0);
        String jsonStr = JSON.toJSONString(map);
        out.write(jsonStr);
    }

}
